<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>权限分配</title>
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
<link rel="stylesheet" href="__PUBLIC__/admin/css/admin.css">
</head>
<body class="layui-bg-gray">
	<div class="layui-fluid">
		<div class="layui-row layui-col-space10">
			<div class="layui-col-md12 layui-col-xs12">
				<div class="layui-card">
					<div class="layui-card-body">
						<form class="layui-form" action="">
							<div id="menus"
								style="width: 400px; border: 1px dashed #eee; padding: 10px 0 25px 5px;">
							</div>
							<div class="layui-form-item"
								style="margin: 10px; text-align: right;">
								<div class="layui-input-block">
									<button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script src="__PUBLIC__/layui/layui.js"></script>
	<script src="__PUBLIC__/layui/layui-xtree.js"></script>
	<script type="text/javascript"
		src="__PUBLIC__/common/js/jquery-2.0.3.min.js"></script>
	<script type="text/javascript">
		var menus = {$menus};
		var privileges = {$privileges};
		var nid = {$nid};
		$(function() {
			layui.use('form', function() {
				var form = layui.form;
				var menusTree = new layuiXtree({
					elem : 'menus',
					form : form,
					isopen : false,
					data : menus
				});
				form.on('submit(formDemo)', function(data) {
					var cbs = menusTree.GetAllChecked();
					var info = [];
					for(var i = 0; i < cbs.length; i++) {
						var cb = cbs[i];
						var obj = {
							menu_id : cb.value,
							privilege_id : nid
						};
						var parent = menusTree.GetParent(cb.pid);
						while(parent) {
							var obj = {
								menu_id : parent.value,
								privilege_id : nid
							};
							info.push(obj);
							parent = menusTree.GetParent(parent.pid);
						}
						info.push(obj);
					}
					if(info.length <= 0) {
						layer.msg('权限无更改');
						return false;
					}
					var index = layer.load(3);
					$.ajax({
						type : "POST",
						url : "{:U('Limits/privilegeAllot')}",
						data : {
							info : info,
							nid : nid
						},
						success : function(res) {
							layer.close(index);
							if (res.status == 1) {
								layer.msg(res.info, {time : 1500},function(){
									location.reload();
								});
								layui.data('cache', {
									key : 'status',
									value : true
								});
							} else {
								layer.msg(res.info);
							}
						}
					});
					return false;
				});
			});
		});
	</script>
</body>
</html>